package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.ServiceInfoVisitorClient;
import com.scada.model.bean.ServiceInfo;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.ServiceInfoVO;
import com.scada.model.dataTable.ChooseServiceList;
import com.scada.model.dataTable.ServiceInfoList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.result.RestResponseBean;
import com.scada.model.search.ServiceInfoSearch;

@Controller
@RequestMapping("serviceInfo")
public class ServiceInfoController extends BaseController{

	@Autowired
	private ServiceInfoVisitorClient serviceInfoVisitorClient;
	
	@RequestMapping(value = "toMemberMain")
	public String toMemberMain(){
		return "service/member/list";
	}
	
	@RequestMapping(value = "getMemberServicePageData")
	@ResponseBody
	public String getMemberServicePageData(@ModelAttribute ServiceInfoSearch serviceInfoSearch) {
		
		PageInfo<ServiceInfoList> pageInfo = new PageInfo<ServiceInfoList>();
        pageInfo.setStart(serviceInfoSearch.getStart());
        pageInfo.setPageSize(serviceInfoSearch.getLength());
        pageInfo.setDraw(serviceInfoSearch.getDraw());
        serviceInfoSearch.setServiceType(ServiceInfo.ServiceTypeCode.MEMBER.getKey());
        ResponseBean rb = serviceInfoVisitorClient.getServiceInfoPageData(serviceInfoSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<ServiceInfoList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<ServiceInfoList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toAddMemberService")
	public ModelAndView toAddMemberService(ModelMap model){
		
		model.put("serviceType", ServiceInfo.ServiceTypeCode.MEMBER.getKey());
		return new ModelAndView("service/member/add");
	}
	
	@RequestMapping(value = "insertServiceInfo")
	@ResponseBody
	public String insertServiceInfo(@ModelAttribute ServiceInfo serviceInfo){
		
		ResponseBean rb = new ResponseBean();
		
		Boolean re = true;
		rb = serviceInfoVisitorClient.repeatNameValidate(null, serviceInfo.getServiceName(),serviceInfo.getServiceType());
		if(StringUtils.equals("2", rb.getType())){
			Integer count = (Integer)rb.getObject("result");
			if(count>=1){
				re = false;
				rb.failed("服务名称重复，无法添加！");
			}
		}else{
			re = false;
			rb.failed("服务名称重复验证失败，无法添加！");
		}
		
		if(re){
			User user = getCurrentUser();
			serviceInfo.setCreateId(user.getId());
			serviceInfo.setLastUpdateId(user.getId());
			rb = serviceInfoVisitorClient.insertServiceInfo(serviceInfo);
		}
		return toJson(rb);
	}
	
	@RequestMapping(value = "toEditServiceInfo")
	public ModelAndView toEditServiceInfo(Long id, ModelMap model){
		
		ServiceInfoVO siv = null;
		try {
			ResponseBean rb = serviceInfoVisitorClient.getServiceInfoById(id);
			if(StringUtils.equals("2", rb.getType())){
				siv = (ServiceInfoVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("siv", siv);
		if(siv != null) {
			if(StringUtils.equals(ServiceInfo.ServiceTypeCode.MEMBER.getKey(), siv.getServiceType())) {
				return new ModelAndView("service/member/edit");
			}else if(StringUtils.equals(ServiceInfo.ServiceTypeCode.PROJECT.getKey(), siv.getServiceType())) {
				return new ModelAndView("service/project/edit");
			}
		}
		
		return null;
		
	}
	
	
	@RequestMapping(value = "updateServiceInfo")
	@ResponseBody
	public String updateServiceInfo(@ModelAttribute ServiceInfo serviceInfo){
		
		ResponseBean rb = new ResponseBean();
		
		Boolean re = true;
		rb = serviceInfoVisitorClient.repeatNameValidate(serviceInfo.getId(), serviceInfo.getServiceName(),serviceInfo.getServiceType());
		if(StringUtils.equals("2", rb.getType())){
			Integer count = (Integer)rb.getObject("result");
			if(count>=1){
				re = false;
				rb.failed("服务名称重复，无法添加！");
			}
		}else{
			re = false;
			rb.failed("服务名称重复验证失败，无法添加！");
		}
		
		if(re){
			User user = getCurrentUser();
			serviceInfo.setLastUpdateId(user.getId());
			rb = serviceInfoVisitorClient.updateServiceInfo(serviceInfo);
		}
		return toJson(rb);
	}
	
	@RequestMapping(value = "toProjectMain")
	public String toProjectMain(){
		return "service/project/list";
	}
	
	@RequestMapping(value = "getProjectServicePageData")
	@ResponseBody
	public String getProjectServicePageData(@ModelAttribute ServiceInfoSearch serviceInfoSearch) {
		
		PageInfo<ServiceInfoList> pageInfo = new PageInfo<ServiceInfoList>();
        pageInfo.setStart(serviceInfoSearch.getStart());
        pageInfo.setPageSize(serviceInfoSearch.getLength());
        pageInfo.setDraw(serviceInfoSearch.getDraw());
        serviceInfoSearch.setServiceType(ServiceInfo.ServiceTypeCode.PROJECT.getKey());
        ResponseBean rb = serviceInfoVisitorClient.getServiceInfoPageData(serviceInfoSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<ServiceInfoList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<ServiceInfoList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toAddProjectService")
	public ModelAndView toAddProjectService(ModelMap model){
		
		model.put("serviceType", ServiceInfo.ServiceTypeCode.PROJECT.getKey());
		return new ModelAndView("service/project/add");
	}
	
	@RequestMapping(value = "deleteServiceInfoById")
	@ResponseBody
	public String deleteServiceInfoById(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			serviceInfoVisitorClient.deleteServiceInfoById(ServiceInfo.IsShowCode.NO.getKey(), ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
	
	@RequestMapping("getSelectMemberService")
	@ResponseBody
	public String getSelectMemberService(String pageSize,String currentPage,String searchName){
		
		RestResponseBean rest=RestResponseBean.getRestResponseBean();
		ResponseBean responseBean=null;
		try{
			responseBean=serviceInfoVisitorClient.getSelectService(pageSize, currentPage, searchName, ServiceInfo.ServiceTypeCode.MEMBER.getKey());
			if(StringUtils.equals("2", responseBean.getType())){
				rest.setCount((Integer)responseBean.getObject("resultCount"));
				rest.setData((List<ChooseServiceList>)responseBean.getObject("resultList"));
	        }else{
	        	rest.setCount(0);
	        	rest.setData(new ArrayList<ChooseServiceList>());
	        }
		}catch(Exception e){
			rest.setCount(0);
        	rest.setData(new ArrayList<ChooseServiceList>());
		}
		rest.success();
		
		return toJson(rest);
	}
	
	@RequestMapping("getSelectProjectService")
	@ResponseBody
	public String getSelectProjectService(String pageSize,String currentPage,String searchName){
		
		RestResponseBean rest=RestResponseBean.getRestResponseBean();
		ResponseBean responseBean=null;
		try{
			responseBean=serviceInfoVisitorClient.getSelectService(pageSize, currentPage, searchName, ServiceInfo.ServiceTypeCode.PROJECT.getKey());
			if(StringUtils.equals("2", responseBean.getType())){
				rest.setCount((Integer)responseBean.getObject("resultCount"));
				rest.setData((List<ChooseServiceList>)responseBean.getObject("resultList"));
	        }else{
	        	rest.setCount(0);
	        	rest.setData(new ArrayList<ChooseServiceList>());
	        }
		}catch(Exception e){
			rest.setCount(0);
        	rest.setData(new ArrayList<ChooseServiceList>());
		}
		rest.success();
		
		return toJson(rest);
	}
	
	@RequestMapping("getServiceInfoById")
	@ResponseBody
	public String getServiceInfoById(Long id){
		
		RestResponseBean rest=RestResponseBean.getRestResponseBean();
		try{
			ResponseBean rb = serviceInfoVisitorClient.getServiceInfoById(id);
			ServiceInfoVO siv = null;
			if(StringUtils.equals("2", rb.getType())){
				siv = (ServiceInfoVO)rb.getObject("result");
			}
			rest.setData(siv);
		}catch(Exception e){
			rest.setData(null);
		}
		rest.success();
		return toJson(rest);
	}
	
	@RequestMapping("getSelectServiceInfo")
	@ResponseBody
	public String getSelectServiceInfo(String pageSize,String currentPage,String searchName){
		
		RestResponseBean rest=RestResponseBean.getRestResponseBean();
		ResponseBean responseBean=null;
		try{
			responseBean=serviceInfoVisitorClient.getSelectService(pageSize, currentPage, searchName, "");
			if(StringUtils.equals("2", responseBean.getType())){
				rest.setCount((Integer)responseBean.getObject("resultCount"));
				rest.setData((List<ChooseServiceList>)responseBean.getObject("resultList"));
	        }else{
	        	rest.setCount(0);
	        	rest.setData(new ArrayList<ChooseServiceList>());
	        }
		}catch(Exception e){
			rest.setCount(0);
        	rest.setData(new ArrayList<ChooseServiceList>());
		}
		rest.success();
		
		return toJson(rest);
	}
	
}
